﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32.TaskScheduler;

namespace SQLServerOptimizer.PL.WPF
{
    /// <summary>
    /// Interaction logic for SchedulingControl.xaml
    /// </summary>
    public partial class SchedulingControl : UserControl
    {
        public static readonly DependencyProperty timeProperty =
    DependencyProperty.Register("timech", typeof(string),
    typeof(SchedulingControl), new UIPropertyMetadata("12:00"));

        public string timech
        {
            get { return (string)GetValue(timeProperty); }
            set { SetValue(timeProperty, value); }
        }


        public SchedulingControl()
        {
            this.InitializeComponent();
        }

        DateTime time;
        int type;
        int option;
        Int16 interval;
        List<DaysOfTheWeek> daysOfWeek;
        int dayOfMonth = 0;
        WhichWeek weeksOfMonth;
        DaysOfTheWeek daysOfTheWeek;
        MonthsOfTheYear monthsOfTheYear;

        private void rbDaily_Checked(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
            {
                spDaily.Visibility = Visibility.Visible;
                spWeekly.Visibility = Visibility.Hidden;
                spMonthly.Visibility = Visibility.Hidden;
            }
        }

        private void rbWeekly_Checked(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
            {
                spDaily.Visibility = Visibility.Hidden;
                spWeekly.Visibility = Visibility.Visible;
                spMonthly.Visibility = Visibility.Hidden;
            }
        }

        private void rbMonthly_Checked(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
            {
                spDaily.Visibility = Visibility.Hidden;
                spWeekly.Visibility = Visibility.Hidden;
                spMonthly.Visibility = Visibility.Visible;
            }
        }

        public void createtask()
        {
            daysOfWeek = new List<DaysOfTheWeek>();
            time = Convert.ToDateTime(this.textboxtime.Text.ToString());
            if ((bool)rbDaily.IsChecked)
            {
                type = 1;
                if ((bool)rbEverynumberofdays.IsChecked)
                {
                    option = 1;
                    interval = Convert.ToInt16(tbxNumberOfDays.Text);
                }
                else if ((bool)rbEveryWeekDay.IsChecked)
                {
                    option = 2;
                    interval = 1;
                    daysOfWeek.Add(DaysOfTheWeek.Monday);
                    daysOfWeek.Add(DaysOfTheWeek.Tuesday);
                    daysOfWeek.Add(DaysOfTheWeek.Wednesday);
                    daysOfWeek.Add(DaysOfTheWeek.Thursday);
                    daysOfWeek.Add(DaysOfTheWeek.Friday);
                }
            }
            else if ((bool)rbWeekly.IsChecked)
            {
                interval = Convert.ToInt16(weeklyInterval.Text);
                type = 2;
                if ((bool)chbxMonday.IsChecked)
                {
                    daysOfWeek.Add(DaysOfTheWeek.Monday);
                }
                if ((bool)chbxTuesday.IsChecked)
                {
                    daysOfWeek.Add(DaysOfTheWeek.Tuesday);
                }
                if ((bool)chbxWednesday.IsChecked)
                {
                    daysOfWeek.Add(DaysOfTheWeek.Wednesday);
                }
                if ((bool)chbxThursday.IsChecked)
                {
                    daysOfWeek.Add(DaysOfTheWeek.Thursday);
                }
                if ((bool)chbxFriday.IsChecked)
                {
                    daysOfWeek.Add(DaysOfTheWeek.Friday);
                }
                if ((bool)chbxSaturday.IsChecked)
                {

                    daysOfWeek.Add(DaysOfTheWeek.Saturday);
                }
                if ((bool)chbxSunday.IsChecked)
                {

                    daysOfWeek.Add(DaysOfTheWeek.Sunday);

                }




            }
            else if ((bool)rbMonthly.IsChecked)
            {
                type = 3;
                if ((bool)rbEverynumberofdayinmonth.IsChecked)
                {
                    option = 1;
                    dayOfMonth = Convert.ToInt32(tbxNumberOfDay.Text);
                    monthsOfTheYear = MonthsOfTheYear.AllMonths;


                }
                else if ((bool)rbTheDayinMonth.IsChecked)
                {
                    option = 2;
                    monthsOfTheYear = MonthsOfTheYear.AllMonths;
                    switch (daysofweekbox.SelectedIndex)
                    {
                        case 0:
                            weeksOfMonth = WhichWeek.FirstWeek;
                            break;
                        case 1:
                            weeksOfMonth = WhichWeek.SecondWeek;
                            break;
                        case 2:
                            weeksOfMonth = WhichWeek.ThirdWeek;
                            break;
                        case 3:
                            weeksOfMonth = WhichWeek.FourthWeek;
                            break;
                        case 4:
                            weeksOfMonth = WhichWeek.LastWeek;
                            break;
                        default:
                            break;
                    }
                    switch (dayssbox.SelectedIndex)
                    {
                        case 0:
                            daysOfTheWeek = DaysOfTheWeek.AllDays;
                            break;
                        case 1:
                            daysOfTheWeek = DaysOfTheWeek.Monday;
                            break;
                        case 2:
                            daysOfTheWeek = DaysOfTheWeek.Tuesday;
                            break;
                        case 3:
                            daysOfTheWeek = DaysOfTheWeek.Wednesday;
                            break;
                        case 4:
                            daysOfTheWeek = DaysOfTheWeek.Thursday;
                            break;
                        case 5:
                            daysOfTheWeek = DaysOfTheWeek.Friday;
                            break;
                        case 6:
                            daysOfTheWeek = DaysOfTheWeek.Saturday;
                            break;
                        case 7:
                            daysOfTheWeek = DaysOfTheWeek.Sunday;
                            break;
                        default:
                            break;
                    }

                }
            


            }
            new ScheduleTask().CreateTask(time, type, option, interval, daysOfWeek, dayOfMonth, weeksOfMonth, daysOfTheWeek, monthsOfTheYear);


        }
    }
}